package com.yzh.pinboard.CustomView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.yzh.pinboard.R;


/**
 * Created by Administrator on 2017/2/17.
 */

/**
 *
 */
public class NavView extends LinearLayout implements View.OnClickListener, CompoundButton
        .OnCheckedChangeListener {
    private Context mContext;
    private OnNavViewClickListener mNavViewClickListener;
    private PaintSettingView mPaintSettingView;
    private EraserSettingView mEraserSettingView;
    private int mPaintSettingWidth;
    private CheckBox mPaint;
    private CheckBox mEraser;
    private CheckBox mSelectRect;
    private Button mUndo;
    private Button mRedo;
    private boolean mUndoable;
    private boolean mRedoable;

    public NavView(Context context) {
        super(context);
        init(context);
    }

    public NavView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }


    private void init(Context context) {
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_nav, this);
        mPaintSettingView = (PaintSettingView) view.findViewById(R.id.paintSetting);
        mEraserSettingView = (EraserSettingView) view.findViewById(R.id.eraserSetting);

        mPaint = (CheckBox) view.findViewById(R.id.cb_paint);
        mEraser = (CheckBox) view.findViewById(R.id.cb_eraser);
        mSelectRect = (CheckBox) view.findViewById(R.id.cb_select_rect);
        mUndo = (Button) view.findViewById(R.id.btn_undo);
        mRedo = (Button) view.findViewById(R.id.btn_redo);
        mPaint.setOnCheckedChangeListener(this);
        mEraser.setOnCheckedChangeListener(this);
        mSelectRect.setOnCheckedChangeListener(this);
        mUndo.setOnClickListener(this);
        mRedo.setOnClickListener(this);
    }

    private void setCheckableFalseExcept(CheckBox checkBox) {
        mPaint.setChecked(false);
        mEraser.setChecked(false);
        mSelectRect.setChecked(false);
        checkBox.setChecked(true);

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_undo) {
            if (mNavViewClickListener != null && mUndoable) {
                mNavViewClickListener.onUndo();
            }
        } else if (i == R.id.btn_redo) {
            if (mNavViewClickListener != null && mRedoable) {
                mNavViewClickListener.onRedo();
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mPaintSettingWidth == 0) {
            ViewGroup.LayoutParams params = mPaintSettingView.getLayoutParams();
            mPaintSettingWidth = (int) (mPaintSettingView.getMeasuredWidth() * 1.5);
            params.width = mPaintSettingWidth;
            mPaintSettingView.setLayoutParams(params);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int i = buttonView.getId();
        if (i == R.id.cb_paint) {
            if (isChecked) {
                setCheckableFalseExcept(mPaint);
                mPaintSettingView.setVisibility(VISIBLE);
            } else {
                mPaintSettingView.setVisibility(GONE);
            }
        } else if (i == R.id.cb_eraser) {
            if (isChecked) {
                setCheckableFalseExcept(mEraser);

                mEraserSettingView.setVisibility(VISIBLE);
            } else {
                mEraserSettingView.setVisibility(GONE);
            }
        } else if (i == R.id.cb_select_rect) {
            if (mNavViewClickListener == null) return;
            if (isChecked) {
                setCheckableFalseExcept(mSelectRect);

                mNavViewClickListener.onSelectRect();
            }
        }
    }

    public interface OnNavViewClickListener {

        void onUndo();

        void onRedo();

        void onSelectRect();

    }

    public void setNavViewClickListener(OnNavViewClickListener navViewClickListener) {
        mNavViewClickListener = navViewClickListener;
    }

    public void setUnDoable(boolean undoable) {
        mUndoable = undoable;
        if (undoable) {
            mUndo.setBackgroundResource(R.drawable.undo_able);
        } else {
            mUndo.setBackgroundResource(R.drawable.undo_disable);

        }
    }

    public void setReDoable(boolean redoable) {
        mRedoable = redoable;
        if (redoable) {
            mRedo.setBackgroundResource(R.drawable.redo_able);
        } else {
            mRedo.setBackgroundResource(R.drawable.redo_disable);

        }
    }

    public void setEraserListener(EraserSettingView.EraserListener listener) {
        mEraserSettingView.setEraserListener(listener);
    }

    public void setPaintListener(PaintSettingView.PaintSettingListener listener) {
        mPaintSettingView.setPaintSettingListener(listener);
    }
}
